package com.cloud.controller;

import com.cloud.dto.ArticleDemo;
import com.cloud.entity.UserEntity;
import com.cloud.mapper.UserMapper;
import com.cloud.service.TestService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin
public class TestController {
    List<ArticleDemo> articleDemos = new ArrayList<>();
    @Resource
    private UserMapper userMapper;
    @Resource
    private TestService testService;

    /**
     * @return
     */
    @GetMapping("/key")
    public List<UserEntity> key() {
        List<UserEntity> byUsers = userMapper.getByUsers();
        System.out.println(byUsers);
        return byUsers;
    }


    @GetMapping("/lock/stock")
    public void lock() {
        testService.lock();
    }

    @GetMapping("/before/test/getData")
    public List<ArticleDemo> getData() {
        return articleDemos;

    }

    @PostMapping("/before/test/addData")
    public void addData(@RequestBody ArticleDemo articleDemo) {
        articleDemos.add(articleDemo);
    }

    @PostMapping("/before/test/getDataPost")
    public List<ArticleDemo> getDataPost(@RequestBody ArticleDemo articleDemo) {
        List<ArticleDemo> rest = new ArrayList<>();
        if(CollectionUtils.isEmpty(articleDemos)){
            return null;
        }
        for (ArticleDemo articleDemo1 : articleDemos) {
            if(StringUtils.isNotBlank(articleDemo.getCategory())){
                if(articleDemo1.getCategory().equals(articleDemo.getCategory())){
                    rest.add(articleDemo1);
                }
            }
            if(StringUtils.isNotBlank(articleDemo.getState())){
                if(articleDemo1.getState().equals(articleDemo.getState())){
                    rest.add(articleDemo1);
                }
            }
        }
        return rest;
    }
}
